/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.domain.action.role;

import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.ms.portalcenter.domain.das.RoleDEDas;
import cn.hallele.ms.portalcenter.domain.das.UserRoleDEDas;
import cn.hallele.ms.portalcenter.domain.domain.RoleDomain;
import cn.hallele.ms.portalcenter.domain.domain.UserRoleDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/10/6 13:54
 */
@Slf4j
@Service
public class RoleDomainService {
    @Autowired
    private RoleDEDas roleDEDas;

    @Autowired
    private UserRoleDEDas userRoleDEDas;

    /**
     * 通过用户id查询角色信息
     *
     * @param userId 用户id
     * @return 角色信息
     */
    public List<RoleDomain> findByUserId(Long userId) {
        List<UserRoleDomain> userRoleDomains = userRoleDEDas.findByUserId(userId);

        if (ObjectUtil.isEmpty(userRoleDomains)) {
            return new ArrayList<>();
        }

        Set<Integer> roleIds = userRoleDomains.stream().map(UserRoleDomain::getRoleId).collect(Collectors.toSet());
        return roleDEDas.findByIdIn(roleIds);
    }
}
